package tech.swh.ssm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONObjectCodec;

import tech.swh.ssm.entity.App;
import tech.swh.ssm.entity.Category;
import tech.swh.ssm.entity.Dev;
import tech.swh.ssm.service.AppService;
import tech.swh.ssm.service.CategoryService;
import tech.swh.ssm.service.DictionaryService;
import tech.swh.ssm.service.VersionService;

@Controller
@RequestMapping("/sys/dev")
public class DevController {

	@Autowired
	private AppService appservice;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private VersionService versionService;
	
	
	@RequestMapping(value = "goDevApplist.html",method = RequestMethod.GET)
	public String goDevApplist(Model model,HttpServletRequest request) {
		int createby =((Dev)request.getSession().getAttribute("user")).getId();
		model.addAttribute("app_Version_List", appservice.getAppList(null, 0, 0, 0, 0,createby,0));
		model.addAttribute("dictionaryList", dictionaryService.getDictionayList("APP_FLATFORM"));
		model.addAttribute("categoryList", categoryService.getCategoryList(0));
		return "dev-applist";
	}
	
	@RequestMapping(value = "devAppQuery.action",method = RequestMethod.POST)
	public String appQuery(String appName,Integer appstatus,Integer dictionary,Integer onelevel,@RequestParam(value = "twolevel",required = false) Integer twolevel,@RequestParam(value = "threelevel",required = false) Integer threelevel,Model model,HttpServletRequest request) {
		if (twolevel==null)twolevel=0;
		if (threelevel==null)threelevel=0;
		int createby =((Dev)request.getSession().getAttribute("user")).getId();
		System.out.println("66"+appstatus+"66");
		model.addAttribute("app_Version_List",  appservice.getAppList(appName, dictionary, onelevel, twolevel, threelevel,createby,appstatus));
		model.addAttribute("dictionaryList", dictionaryService.getDictionayList("APP_FLATFORM"));
		model.addAttribute("categoryList", categoryService.getCategoryList(0));
		return "dev-applist";
	}
	
	@RequestMapping(value = "devGoAppStatus.html/{appId}",method = RequestMethod.GET)
	public String goAppStatus(@PathVariable Integer appId,Model model) {						//APP查看
		model.addAttribute("app_Version", appservice.getApp_Version(appId));
		model.addAttribute("dictionaryList", dictionaryService.getDictionayList("APP_FLATFORM"));
		model.addAttribute("categoryList", categoryService.getCategoryList(0));
		model.addAttribute("versionList", versionService.getVersionList(appId));
		return "dev-appStatus";
	}
	
	
	
	
	
	@ResponseBody
	@RequestMapping(value = "getCategory.ajax",method = RequestMethod.POST)
	public String getCategory(Integer parentid) {												//三级联动
		JSONObject result = new JSONObject();
		List<Category> catelist = categoryService.getCategoryList(parentid);
		result.put("catelist", catelist);
		return result.toJSONString();
	}

	
}
